/**************************************************************************************
*  console.c
*
*  This file defines all native methods that have to be implemented to support
*  the class lpc2xxx/hw/Console
*
*  Application developer should use the following functions to get access to
*  arrays and String object:
*       vmGetStringCount
*       vmGetStringArray
*       vmGetArraySize
*       vmGetArray
*  These functions are accessible via vm_config_t structure.
*
*  Native methods implementation
*  -----------------------------
*  Note that if the native method is NOT declared as static then param[0]
*  contains object's reference and must be skiped when accessing the native
*  method parameters.
**************************************************************************************
*
* This file is covered by the GNU GPL with the following exception:
*   As a special exception, the copyright holders of this library give you permission
*   to link this library with independent modules to produce an executable, regardless
*   of the license terms of these independent modules, and to copy and distribute the
*   resulting executable under terms of your choice, provided that you also meet, for
*   each linked independent module, the terms and conditions of the license of that
*   module. An independent module is a module which is not derived from or based on
*   this library. If you modify this library, you may extend this exception to your
*   version of the library, but you are not obligated to do so. If you do not wish
*   to do so, delete this exception statement from your version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
**************************************************************************************/
#include "javavm.h"
#include "uart.h"

extern vm_config_t vmconfig;
static uart_t *uart = NULL;

void setJavaConsoleUart(uart_t *u)
{
    uart = u;
}
/****************************************************************************
* Prints the text to the console's standard output.
* On entry: param[0] - reference to string object.
*
* Java prototype:
*   public static void println(String s)
*
****************************************************************************/
int16 lpc2xxx_hw_Console_print(int32 param[], int32 *retval)
{
    char sb[80];
    jchar *arr;
    int i, count;

    count = (int)vmconfig.vmGetStringCount(param[0]);
    if (count > (jint)sizeof(sb) - 1)
        count = sizeof(sb) - 1;
    arr = vmconfig.vmGetStringArray(param[0]);
    for (i=0; i < count; i++)
        sb[i] = (char)arr[i];
    sb[count] = 0;
    if (uart)
        uartSendString(uart, sb);

    return NO_EXCEP;
}

/****************************************************************************
* Reads one character from the console's standard input.
* On return function sets the retval to zero if no key is available. If a key
* is available function returns its value via the retval parameter.
*
* Java prototype:
*   public static native char getKey()
*
****************************************************************************/
int16 lpc2xxx_hw_Console_getKey(int32 param[], int32 *retval)
{
    uint8 ch = 0;

    if (uart)
        uartGetChar(uart, &ch);
    *retval = ch;

    return NO_EXCEP;
}

/****************************************************************************
* Writes one character to the console's standard output.
* On entry: param[0] - charcter for stdout
*
* Java prototype:
*   public static native void putch(char ch);
*
****************************************************************************/
int16 lpc2xxx_hw_Console_putch(int32 param[], int32 *retval)
{
    if (uart)
        uartSendCh(uart, (uint8)param[0]);

    return NO_EXCEP;
}

/****************************************************************************
* Checks if there is character available for reading on the console's
* standard input.
* On return function should indicate whether key is available or not via
* the retval parameter.
*
* Java prototype:
*   public static native boolean keyAvail()
*
****************************************************************************/
int16 lpc2xxx_hw_Console_keyAvail(int32 param[], int32 *retval)
{
    *retval = (uart && uartCharAvail(uart));

    return NO_EXCEP;
}


